/*
 * Copyright 2018 peipeihh
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * limitations under the License.
 */
package com.pphh.demo;

import com.pphh.common.CookieUtils;
import com.pphh.common.DemoProperties;
import com.pphh.common.UserInfo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

/**
 * Created by huangyinhuang on 3/15/2018.
 */
@SpringBootApplication
@RestController
public class BrowserApp {

    public static void main(String[] args) {
        SpringApplication.run(BrowserApp.class, args);
    }

    @RequestMapping(value = "/api/user", method = RequestMethod.GET)
    public UserInfo readUserInfoByCookie(HttpServletRequest request) {
        UserInfo user = new UserInfo();

        Cookie cookieUserName = CookieUtils.getCookieByName(request, DemoProperties.cookieUserName);
        if (cookieUserName != null) {
            user.setUserName(cookieUserName.getValue());
        }

        Cookie cookieUserRole = CookieUtils.getCookieByName(request, DemoProperties.cookieUserRole);
        if (cookieUserRole != null) {
            user.setUserRole(cookieUserRole.getValue());
        }

        return user;
    }

}
